﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RSPJ.Windows
{
    public partial class Histogram : Form
    {
        string filename; int band, flag;
        int[,] dataArray;

        private void Histogram_Shown(object sender, EventArgs e)
        {
            double[,,] intArray = new double[1, 1, 1];
            int lines = 0, samples = 0;
            for (int i = 0; i < GlobalData.fileList.Count; i++)
            {
                if (filename == GlobalData.fileList[i].Name)
                {
                    intArray = GlobalData.fileList[i].IntArray;
                    lines = GlobalData.fileList[i].Head.Lines;
                    samples = GlobalData.fileList[i].Head.Samples;
                }
            }

            // 显示直方图
            if (flag == 1)
            {
                Text = "直方图";
                Size = new Size(1000, 600);

                //生成直方图
                Refresh();
                Graphics g = pbx.CreateGraphics();
                int width = pbx.Width;
                int height = pbx.Height;
                g.TranslateTransform(100, 480);
                Pen pen1 = new Pen(Color.Blue, 1);
                Pen pen2 = new Pen(Color.Black, 1);
                g.ScaleTransform(1, -1);

                int[] a = new int[256];

                for (int j = 0; j < lines; j++)
                {
                    for (int k = 0; k < samples; k++)
                    {
                        int n = band - 1;
                        for (int p = 0; p < 256; p++)
                        {
                            if ((int)intArray[n, j, k] == p)
                            {
                                a[p]++;
                                break;
                            }
                        }
                    }
                }


                for (int m = 0; m < 256; m++)
                {

                    Point p1 = new Point(m * 3, 0);
                    Point p2 = new Point(m * 3, a[m] / 100);
                    g.DrawLine(pen1, p1, p2);
                }
                g.DrawLine(pen2, 0, 400, 0, 0);
                g.DrawLine(pen2, 0, 0, 800, 0);

                g.ScaleTransform(1, -1);
                g.DrawString("0", new Font("New Timer", 15), Brushes.Black, new PointF(0, 20));
                g.DrawString("50", new Font("New Timer", 15), Brushes.Black, new PointF(150, 20));
                g.DrawString("100", new Font("New Timer", 15), Brushes.Black, new PointF(300, 20));
                g.DrawString("150", new Font("New Timer", 15), Brushes.Black, new PointF(450, 20));
                g.DrawString("200", new Font("New Timer", 15), Brushes.Black, new PointF(600, 20));
                g.DrawString("250", new Font("New Timer", 15), Brushes.Black, new PointF(750, 20));
                g.DrawString("灰度", new Font("New Timer", 15), Brushes.Black, new PointF(820, -10));

                g.DrawString(" 5000", new Font("New Timer", 15), Brushes.Black, new PointF(-80, -50));
                g.DrawString("10000", new Font("New Timer", 15), Brushes.Black, new PointF(-80, -100));
                g.DrawString("15000", new Font("New Timer", 15), Brushes.Black, new PointF(-80, -150));
                g.DrawString("20000", new Font("New Timer", 15), Brushes.Black, new PointF(-80, -200));
                g.DrawString("25000", new Font("New Timer", 15), Brushes.Black, new PointF(-80, -250));
                g.DrawString("30000", new Font("New Timer", 15), Brushes.Black, new PointF(-80, -300));
                g.DrawString("35000", new Font("New Timer", 15), Brushes.Black, new PointF(-80, -350));
                g.DrawString(" 像元数", new Font("New Timer", 15), Brushes.Black, new PointF(-40, -450));

                pen1.Dispose();
                pen2.Dispose();
            }

            // 显示累计直方图
            if (flag == 2)
            {
                Text = "累计直方图";
                Size = new Size(1000, 600);

                //生成累计直方图
                Refresh();
                Graphics g = pbx.CreateGraphics();
                int width = pbx.Width;
                int height = pbx.Height;
                g.TranslateTransform(100, 480);
                Pen pen1 = new Pen(Color.Red, 1);
                Pen pen2 = new Pen(Color.Black, 1);
                g.ScaleTransform(1, -1);
                int sum = 0;
                int[] a = new int[256];

                for (int j = 0; j < lines; j++)
                {
                    for (int k = 0; k < samples; k++)
                    {
                        int n = band - 1;
                        for (int p = 0; p < 256; p++)
                        {
                            if ((int)intArray[n, j, k] == p)
                            {
                                a[p]++;
                                break;
                            }
                        }
                    }
                }

                for (int m = 0; m < 256; m++)
                {

                    sum += a[m];
                    Point p1 = new Point(m * 3, 0);
                    Point p2 = new Point(m * 3, sum / 1000);
                    g.DrawLine(pen1, p1, p2);

                }
                g.DrawLine(pen2, 0, 400, 0, 0);
                g.DrawLine(pen2, 0, 0, 800, 0);

                g.ScaleTransform(1, -1);
                g.DrawString("0", new Font("New Timer", 15), Brushes.Black, new PointF(0, 20));
                g.DrawString("50", new Font("New Timer", 15), Brushes.Black, new PointF(150, 20));
                g.DrawString("100", new Font("New Timer", 15), Brushes.Black, new PointF(300, 20));
                g.DrawString("150", new Font("New Timer", 15), Brushes.Black, new PointF(450, 20));
                g.DrawString("200", new Font("New Timer", 15), Brushes.Black, new PointF(600, 20));
                g.DrawString("250", new Font("New Timer", 15), Brushes.Black, new PointF(750, 20));
                g.DrawString("灰度", new Font("New Timer", 15), Brushes.Black, new PointF(820, -10));

                g.DrawString(" 50000", new Font("New Timer", 15), Brushes.Black, new PointF(-80, -50));
                g.DrawString("100000", new Font("New Timer", 15), Brushes.Black, new PointF(-80, -100));
                g.DrawString("150000", new Font("New Timer", 15), Brushes.Black, new PointF(-80, -150));
                g.DrawString("200000", new Font("New Timer", 15), Brushes.Black, new PointF(-80, -200));
                g.DrawString("250000", new Font("New Timer", 15), Brushes.Black, new PointF(-80, -250));
                g.DrawString("300000", new Font("New Timer", 15), Brushes.Black, new PointF(-80, -300));
                g.DrawString("350000", new Font("New Timer", 15), Brushes.Black, new PointF(-80, -350));
                g.DrawString("像元数", new Font("New Timer", 15), Brushes.Black, new PointF(-40, -450));

                pen1.Dispose();
                pen2.Dispose();
            }

            // 显示联合直方图，共生矩阵
            if (flag == 3)
            {
                Text = filename;
                Size = new Size(256, 256);
                pbx.Size = new Size(256, 256);
                Bitmap bmp = new Bitmap(256, 256);
                Graphics gp = Graphics.FromImage(bmp);
                gp.Clear(Color.White);
                for (int y = 0; y < 256; y++)
                {
                    for (int x = 0; x < 256; x++)
                    {
                        int r = dataArray[y, x];
                        Color color = Color.FromArgb(r, r, r);

                        gp.FillRectangle(new SolidBrush(color), x, y, 20, 20);
                    }
                }
                gp.Save();

                pbx.Image = bmp;
            }
        }

        public Histogram(string filename, int band, int flag)
        {
            InitializeComponent();
            this.filename = filename;
            this.band = band;
            this.flag = flag;
        }

        public Histogram(int[,] dataArray,string filename, int flag)
        {
            InitializeComponent();
            this.dataArray = dataArray;
            this.flag = flag;
            this.filename = filename;
        }
    }
}
